package com.meichis.yndms.ui.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.google.gson.Gson;
import com.meichis.mcsappframework.rollpager.CommonAdapter;
import com.meichis.mcsappframework.rollpager.hintview.ColorPointHintView;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.yndms.R;
import com.meichis.yndms.databinding.FragmentHomeBinding;
import com.meichis.yndms.model.RotateAdv;
import com.meichis.yndms.ui.activity.MonthBusinessAmountActivity;
import com.meichis.yndms.ui.activity.ProductArrivalActivity;
import com.meichis.yndms.ui.activity.ProductStockActivity;
import com.meichis.yndms.ui.common.BaseFragment;
import com.meichis.yndms.vm.HomeFragmentVM;

public class HomeFragment extends BaseFragment {

    private FragmentHomeBinding binding;
    private HomeFragmentVM vm;

    @Override
    protected View onCreateDataBindingView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(HomeFragmentVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
        return binding.getRoot();
    }

    @Override
    protected void init() {
        String ak = (String) SharePreferenceUtil.getInstance().getCacheValue(SharePreferenceUtil.PREFERENCES_AUTHKEY);
        Log.d("插件打印", "init: " + ak);
        binding.tvClientName.setText(vm.getUserInfo().getValue().getClientFullName());
        //防止内容过长自动滑动到底部
        binding.llContent.setFocusable(false);
        binding.llContent.setFocusableInTouchMode(true);
        binding.llContent.requestFocus();
        addObserver();
        vm.getRotateAdv().setValue((RotateAdv) util.getObjectValue(SharePreferenceUtil.PREFERENCES_ROTATEADV));
        vm.GetOverview();
        //下拉刷新
        binding.swip.setOnRefreshListener(() -> {
            binding.swip.setRefreshing(false);
            vm.GetOverview();
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        binding.webBzbffgl.onResume();
        binding.webSalesSummary.onResume();
    }

    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getBzbffgl().observe(this, bzbffgl -> {
            binding.webBzbffgl.setBackgroundColor(Color.parseColor("#ffffff"));
            //加载本地资源文件
            binding.webBzbffgl.loadUrl("file:///android_asset/HomeFragment_bzbffgl.html");
            binding.webBzbffgl.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    binding.webBzbffgl.sendResult("", "JavaCallJs", bzbffgl);
                }
            });
        });

        vm.getSalesSummaryList().observe(this, salesSummaries -> {

            binding.webSalesSummary.setBackgroundColor(Color.parseColor("#ffffff"));
            //加载本地资源文件
            binding.webSalesSummary.loadUrl("file:///android_asset/HomeFragment_SalesSummary.html");
            binding.webSalesSummary.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    super.onPageFinished(view, url);
                    binding.webSalesSummary.sendResult("", "JavaCallJs", new Gson().toJson(salesSummaries));
                }
            });
        });

        vm.getRotateAdv().observe(this, rotateAdv -> {
            if (rotateAdv != null && rotateAdv.getItems().size() > 0) {
                String[] imageUrls = new String[rotateAdv.getItems().size()];
                for (int i = 0; i < rotateAdv.getItems().size(); i++) {
                    imageUrls[i] = rotateAdv.getItems().get(i).getImageURL();
                }
                CommonAdapter pagerAdapter = new CommonAdapter(getActivity(), imageUrls, binding.rpv, 32);
                binding.rpv.setPlayDelay(rotateAdv.getTimespan());
                binding.rpv.setAnimationDurtion(500);//0.5s切换动画时长
                binding.rpv.setAdapter(pagerAdapter);
                binding.rpv.setHintView(new ColorPointHintView(getActivity(), Color.BLUE, Color.GRAY));

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
        binding.webSalesSummary.onPause();
        binding.webBzbffgl.onPause();
    }

    public class ClickEvent {

        public void onSelectMonthProductStandingByBrand() {
            openActivity(ProductStockActivity.class);
        }

        public void onSelectMonthProductArrivalByBrand() {
            openActivity(ProductArrivalActivity.class);
        }

        public void onSelectMonthPurchaseAmount() {
            Intent intent = new Intent();
            intent.putExtra("Tag", 1);
            openIntentActivity(MonthBusinessAmountActivity.class, intent);
        }

        public void onSelectMonthSaleAmount() {
            Intent intent = new Intent();
            intent.putExtra("Tag", 2);
            openIntentActivity(MonthBusinessAmountActivity.class, intent);
        }
    }
}
